/*
 * Copyright By Peak3
 * Copyright By c336bf6ff03a756989bb456b5b5e3271228d43ed38461eb5cb5755d17a2fc9b8
 * All rights reserved.
 *
 * If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or
 * dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your system.
 */

package com.vv.tools.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.vv.tools.common.constant.base.YesNoEnum;
import com.vv.tools.common.util.TraceOp;

import java.time.LocalDateTime;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author liwen
 */
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        String userId = getUserIdFromTrace();
        this.strictInsertFill(metaObject, "creator", String.class, userId);
        this.strictInsertFill(metaObject, "gmtCreated", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "modifier", String.class, userId);
        this.strictInsertFill(metaObject, "gmtModified", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "isDeleted", String.class, YesNoEnum.YES.getDbView());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userId = getUserIdFromTrace();
        this.strictUpdateFill(metaObject, "modifier", String.class, userId);
        this.strictUpdateFill(metaObject, "gmtModified", LocalDateTime.class, LocalDateTime.now());
    }

    private String getUserIdFromTrace() {
        return String.valueOf(TraceOp.getUserId());
    }

}
